import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';
import 'package:xiongan_app_yy/provider/libback_provider.dart';

class LibBack extends StatefulWidget {
  @override
  _LibBackState createState() => _LibBackState();
}

class _LibBackState extends State<LibBack> {
  List<dynamic> _data = List();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      Provider.of<LibBackProvider>(context)
          .getList(sendObj: {"pageno": 1, "pageSize": -1, "orderno": ""});
    });
  }

  @override
  Widget build(BuildContext context) {
    _data = Provider.of<LibBackProvider>(context).listData;
    return Scaffold(
      appBar: AppBar(
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
        backgroundColor: Color(0XFF00A5F1),
        title: Text(
          "样品回库",
          style: TextStyle(fontSize: 18),
        ),
        actions: <Widget>[
          new IconButton(
            // action button
            padding: EdgeInsets.only(),
            iconSize: 22,
            icon: new Icon(IconData(0xe617, fontFamily: "wuIconFont")),
            onPressed: () {
              Navigator.pushNamed(context, "/libBackSearchPage");
            },
          )
        ],
        elevation: 0,
        centerTitle: true,
      ),
      body: LoadBuilder(
          visibility: Provider.of<LibBackProvider>(context).netStatus1,
          emptyVisibility: !Provider.of<LibBackProvider>(context).netStatus1&&_data.length==0,
          builder: RefreshIndicator(
              child: ListView.separated(
                  itemBuilder: (BuildContext context, int index) {
                    return TextCartItem(
                      data: _data,
                      index: index,
                      onTapFuns: (String id) {
                        Provider.of<LibBackProvider>(context)
                            .getDetailListData(orderid: _data[index]['id']);
                        Navigator.of(context).pushNamed("/lib_back_list",
                            arguments: _data[index]['id']);
                      },
                    );
                  },
                  separatorBuilder: (BuildContext context, int index) {
                    return Divider(color: Colors.black26);
                  },
                  itemCount: _data.length),
              onRefresh: _refresh)),
    );
  }

  Future<void> _refresh() async {
    await Future.delayed(Duration(seconds: 1), () {
      return null;
    });
    Provider.of<LibBackProvider>(context)
        .getList(sendObj: {"pageno": 1, "pageSize": -1, "orderno": ""});
  }
}

class _Text extends Text {
  TextStyle style;

  _Text(String data,
      {this.style = const TextStyle(
        color: Color(0xff646464),
        fontSize: 14,
      )})
      : super(data, style: style);
}

class TextCartItem extends StatelessWidget {
  final List<dynamic> data;
  final int index;
  final Function(String id) onTapFuns;

  TextCartItem(
      {Key key, @required this.data, @required this.index, this.onTapFuns})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Color textColor = data[index]['gobackstate'] == "待回库"
        ? Color(0xffF5BD36)
        : data[index]['gobackstate'] == "回库中"
            ? Color(0xFF2FC480)
            : Color(0xFF00A5F1);
    return GestureDetector(
      child: Container(
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
              //解决GestureDetector Widget事件触发面问题
              border: Border.all(color: Colors.transparent, width: 1)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              _Text("回库任务号：${data[index]['gobacktaskno']}"),
              Container(
                height: 6,
              ),
              _Text("检测任务号：${data[index]['taskno']}"),
              Container(
                height: 6,
              ),
              RichText(
                text: TextSpan(
                  text: '状态:',
                  style: TextStyle(fontSize: 14.0, color: Color(0xff646464)),
                  children: <TextSpan>[
                    TextSpan(
                      text: data[index]['gobackstate'],
                      style: TextStyle(fontSize: 14.0, color: textColor),
                    ),
                  ],
                ),
              ),
              Container(
                height: 6,
              ),
              _Text("创建时间：${data[index]['gobackCreationTime']}"),
            ],
          )),
      onTap: () {
        if (onTapFuns != null) {
          onTapFuns(data[index]['id']);
        }
      },
    );
  }
}
